package com.yiwu.common.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Getter;
import lombok.Setter;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientAutoConfiguration;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticConfig extends ElasticsearchClientAutoConfiguration {

    private final String serverUrl = "http://114.116.241.37:9200";
    private final String apiKey = "a1ZkRUlwRUJZajV5LXk0LUp1Vlg6Nm1WRDZ2SVVSbktfaTAtQ19TZHQ4Zw==";

    @Getter
    private ElasticsearchClient esClient;

    // And create the API client
    public ElasticConfig() {
        // Create the transport with a Jackson mapper
        RestClient restClient = RestClient
                .builder(new HttpHost(serverUrl))
                .setDefaultHeaders(new Header[]{
                        new BasicHeader("Authorization", "ApiKey " + apiKey),
                })
                .build();
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        esClient = new  ElasticsearchClient(transport);
    }

}
